package com.fzh.wangxiao.utils;


import com.fzh.wangxiao.config.common.Constant;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class TimeUtils {


    /**
     * 获得时间差
     * @param date
     * @return
     */
    public static Long getDay(String date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long days = null;
        try {
            Date currentTime = dateFormat.parse(dateFormat.format(new Date()));//现在系统当前时间
            Date pastTime = dateFormat.parse(date);//过去时间
            days = Math.abs(currentTime.getTime() - pastTime.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }


    /**
     * 获取当前时间
     * @return
     */
    public static String getSignsDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());
    }


    /**
     * 获取当前时间
     * @return
     */
    public static String getSignsDay2() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        return df.format(new Date());
    }


    /**
     * 计算时间差相差多少
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String bdate){
        long between_days =-1L;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(getSignsDay2()));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            between_days=(time2-time1)/(1000*3600*24);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Integer.parseInt(String.valueOf(between_days));
    }





    /**
     * 获取当前时间
     * @return
     */
    public static int randomReward(int a) {
        Random r =new Random();
        BigDecimal a1 = new BigDecimal(Double.toString((a + 1)));
        BigDecimal b1 = new BigDecimal(Double.toString(Constant.SINGIN_REWARD_MULTIPLE[r.nextInt(11)]));
        return a1.multiply(b1).intValue();
    }


    public static void main(String[] args) {
         Date date = new Date();//获取当前日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化一下
         System.out.println(sdf.format(date).toString());

         Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象

         //在当前时间的基础上获取前三天的日期
         calendar1.add(Calendar.DATE, -3);
//add方法 参数也可传入 月份，获取的是前几月或后几月的日期//calendar1.add(Calendar.MONTH, -3);
         Date today = calendar1.getTime();
         System.out.println(sdf.format(today).toString());
    }


}
